package com.demo.template.config;

import com.demo.template.balancer.MySelfLoadBalancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

public class MySelfLoadBalancedConfig {

    @Bean
    @Primary
    ReactorLoadBalancer<ServiceInstance> mySelfLoadBalancerBean(Environment environment,
                                                            LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new MySelfLoadBalancer(
                name,
                loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class)
                );
    }
}
